package com.jgsu.farm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.Rule;
import com.jgsu.farm.entity.Video;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.exception.FileException;
import com.jgsu.farm.mapper.VideoMapper;
import com.jgsu.farm.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    VideoMapper videoMapper;

    @Override
    public void add(Video video) {
        int insert = videoMapper.insert(video);
        if (insert != 1){
            throw new DataAddException("604","上传视频文件出现错误");
        }
    }

    @Override
    public void delete(Integer videoid) {
        int i = videoMapper.deleteByPrimaryKey(videoid);
        if (i == 0){
            throw new DataNotExistException("603", "不存在该id,视频资源删除失败");
        }
    }

    @Override
    public PageInfo<Video> findAll(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        List<Video> videoList = videoMapper.selectAll();
        PageInfo<Video> pageInfoVideoList = new PageInfo<Video>(videoList);
        return pageInfoVideoList;
    }

    @Override
    public void update(Video video) {
        Video videoId = videoMapper.selectByPrimaryKey(video.getVideoid());
        if (videoId == null){
            throw new DataNotExistException("603", "不存在该id,视频资源更新失败");
        }else {
            videoMapper.updateByPrimaryKey(video);
        }
    }
}
